import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import ltemMusic from '../views/ltemMusic.vue'

//导入state
import store from '@/store/index.js'

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue')
  },
  {
    path: '/itemmusic',
    name: 'ltemMusic',
    component: () => import('../views/ltemMusic.vue')
  },
  // 所搜
  {
    path:'/search',
    name:'/Search',
    component: () => import('../views/Search.vue')
  },
  //登录
  {
    path:'/login',
    name:'/Login',
    component: () => import('../views/Login.vue')
  },
  //登录信息
  {
    path:'/infouser',
    name:'/InfoUser',

    //路由守卫
    beforeEnter: (to, from, next) => {

      //判读state 里的 isLogin 是否等于 true      
                        //判读如果 state 里面有token ，并且 localStorage.getItem 里面也有token 就表示登录过了
      if(store.state.isLogin || store.state.token || localStorage.getItem('token')){
        next()

      }else{
      //如果不等于就跳到登录页面
        next('/login')
      }
      
    },

    component: () => import('../views/InfoUser.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//全局守卫  针对底部组件 判断如果进入login页面，就隐藏。否则显示
router.beforeEach((to, from) => {
  if(to.path == '/login'){
    store.state.isFooterMusic = false;
  }else{
    store.state.isFooterMusic = true;
  }
  // console.log(to)
})

export default router
